package com.xmsx.back.controller.admin;

import com.xmsx.back.pojo.Job;
import com.xmsx.back.results.Result;
import com.xmsx.back.service.admin.JobService;
import com.xmsx.back.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin")
public class JobController {
    @Autowired
    JobService jobService;

    /**
     * 添加招聘信息
     * @param title
     * @param ddl
     * @param website
     * @param email
     * @param content
     * @param datetime
     * @return
     */
    @PutMapping("/job")
    public Result<String> setJob(@RequestParam("title") String title,
                                 @RequestParam("ddl") Date ddl,
                                 @RequestParam("website") String website,
                                 @RequestParam("email") String email,
                                 @RequestParam("content") String content,
                                 @RequestParam("datatime") Date datetime){

        Job job = new Job();
        job.setTitle(title);
        job.setDdl(ddl);
        job.setWebsite(website);
        job.setEmail(email);
        job.setContent(content);
        job.setDatetime(datetime);

        jobService.setJob(job);
        return Result.success("添加成功！");
    }

    /**
     * 修改招聘信息
     * @param id
     * @param title
     * @param ddl
     * @param website
     * @param email
     * @param content
     * @param datetime
     * @return
     */
    @PostMapping("/job")
    public Result<String> updateJob(@RequestParam("id") int id,
                                    @RequestParam("title") String title,
                                    @RequestParam("ddl") Date ddl,
                                    @RequestParam("website") String website,
                                    @RequestParam("email") String email,
                                    @RequestParam("content") String content,
                                    @RequestParam("datatime") Date datetime){
        Job job = new Job();
        job.setId(id);
        job.setTitle(title);
        job.setDdl(ddl);
        job.setWebsite(website);
        job.setEmail(email);
        job.setContent(content);
        job.setDatetime(datetime);

        jobService.updateJob(job);
        return Result.success("修改成功！");
    }

    /**
     * 获取某个招聘信息的所有数据
     * @param id
     * @return
     */
    @GetMapping("/job")
    public Result<Job> getJobById(@RequestParam("id") int id){
        Job job = jobService.getById(id);
        if (job != null) {
            return Result.success(job);
        } else {
            return Result.error("不存在该条信息");
        }
    }

    /**
     * 删除招聘信息
     * @param id
     * @return
     */
    @DeleteMapping("/job")
    public Result<String> deleteJob(@RequestParam("id") int id) {
        Job job = jobService.getById(id);
        if (null == job) {
            // 信息不存在，无法删除
            return Result.error("招聘信息不存在，删除失败");
        }
        jobService.deleteJob(id);
        return Result.success("删除成功！");
    }

    /**
     * 获取招聘信息列表
     * @param pageCount
     * @param size
     * @return
     */
    @GetMapping("/jobList")
    public Result<Map> getJobList(@RequestParam("pageCount") int pageCount, @RequestParam("size") int size) {
        Pager pager = jobService.findByPager(pageCount, size);
        Map myMap = new HashMap<String, String>();
        myMap.put("total", pager.getTotal());
        myMap.put("list", pager.getRows());
        return Result.success(myMap);
    }
}
